package com.hex.liyue.commonmodule.utils;

import android.text.TextUtils;

import com.hex.liyue.commonmodule.net.RequestPacket;


/**
 * Created by yukuo on 2016/4/19.
 * 这是一个加密参数的工具类
 */
public class EncryptionUtil {
    /**
     * 这是一个加密参数的方法
     *
     * @param requestPacket
     * @return
     */
    public static RequestPacket encryptionData(RequestPacket requestPacket) {
        RequestPacket requestParams = new RequestPacket();
        if (requestPacket == null) {
            return null;
        }
        String sig = "UtE5RzJ7IboGDjTS@" + requestPacket.getArgument("query");
        String data = (String) requestPacket.getArgument("data");
        L.i("请求参数", data);
        if (!TextUtils.isEmpty(data)) {
            sig = sig + data;
        }
        try {
            String s = Coder.encryptMD5(sig.getBytes());
            sig = s.toUpperCase();
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            requestParams.addArgument("data", Coder.encryptBASE64(data.getBytes()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        requestParams.url = requestPacket.url;
        requestParams.addArgument("sig", sig);
        requestParams.addArgument("query", requestPacket.getArgument("query"));
        requestParams.addArgument("g", "api");
        return requestParams;
    }

    /**
     * 这是一个加密参数的方法post请求
     *
     * @param requestPacket
     * @return
     */
    public static RequestPacket encryptionDataPOST(RequestPacket requestPacket) {
        RequestPacket requestParams = new RequestPacket();
        if (requestPacket == null) {
            return null;
        }
        String sig = "UtE5RzJ7IboGDjTS@" + requestPacket.getArgument("query");
        String data = (String) requestPacket.getArgument("data");
        if (!TextUtils.isEmpty(data)) {
            sig = sig + data;
        }
        try {
            String s = Coder.encryptMD5(sig.getBytes());
            sig = s.toUpperCase();
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            requestParams.addArgument("data", Coder.encryptBASE64(data.getBytes()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        requestParams.url = requestPacket.url;
        requestParams.addArgument("sig", sig);
        requestParams.addArgument("query", requestPacket.getArgument("query"));
        return requestParams;
    }
}
